package api;

import java.io.IOException;
import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONObject;

import good.Good;
import good.Item;
import redis.clients.jedis.Jedis;

/**
 * 订单相关接口
 * 
 * @author 毛建杰
 *
 */
public class OrderApi
{
	private static Properties properties;
	private static String redisIP;
	private static int redisPort;
	private static String redisPassword;
	private static int redisDb;

	static
	{
		try
		{
			properties = new Properties();
			properties.load(PurseApi.class.getResourceAsStream("/gjolshopapi.properties"));
			redisIP = properties.getProperty("redis.ip");
			redisPort = Integer.parseInt(properties.getProperty("redis.port"));
			redisPassword = properties.getProperty("redis.password");
			redisDb = Integer.parseInt(properties.getProperty("redis.db"));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 游戏向平台发送购买请求
	 * 
	 * @param currency
	 *            支付方式：1.通宝；2.游戏币；3.积分
	 * @param sid
	 *            服务器ID
	 * @param uid
	 *            下单用户ID
	 * @param rid
	 *            下单角色ID
	 * @param rname
	 *            下单角色名称
	 * @param rcuid
	 *            收货用户ID
	 * @param rcid
	 *            收货角色ID
	 * @param rcname
	 *            收货角色名称
	 * @param crtime
	 *            下单时间
	 * @param itmList
	 *            下单商品列表
	 * @param ip
	 *            ip地址
	 */
	public static void shopOrder(int currency, int sid, int uid, BigInteger rid, String rname, int rcuid,
			BigInteger rcid, String rcname, long crtime, ArrayList<Item> itmList, String ip)
	{
		try (Jedis jedis = new Jedis(redisIP, redisPort))
		{
			jedis.auth(redisPassword);
			jedis.select(redisDb);
			JSONObject request = new JSONObject().put("currency", currency).put("sid", sid).put("uid", uid)
					.put("rid", rid).put("rname", rname).put("rcuid", rcuid).put("rcid", rcid).put("rcname", rname)
					.put("crtime", crtime).put("ip", ip);
			JSONArray itm = new JSONArray();
			int totalPrice = 0;
			for (Item item : itmList)
			{
				int level = item.getLevel();
				int price = item.getGood().getPrice(item.getLevel());
				itm.put(new JSONObject().put("id", item.getGood().getGoodsID()).put("num", item.getNumber())
						.put("price", price).put("level", level));
				totalPrice += price * item.getNumber();
			}
			request.put("itm", itm).put("price", totalPrice);
			System.out.println(request.toString());
			jedis.rpush("shop_order", request.toString());
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	/**
	 * 游戏向平台获取购买响应
	 * 
	 * @param sid
	 *            服务器ID
	 * @return 商城下单结果
	 */
	public static String orderResult(int sid)
	{
		try (Jedis jedis = new Jedis(redisIP, redisPort))
		{
			jedis.auth(redisPassword);
			jedis.select(redisDb);
			String response = jedis.lpop("order_result:" + sid);
			return response;
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	/**
	 * 游戏向平台发送购买响应确认
	 * 
	 * @param oid
	 *            订单ID
	 * @param code
	 *            状态码
	 * @param time
	 *            响应确认时间
	 */
	public static void orderResultOK(String oid, int code, long time)
	{
		try (Jedis jedis = new Jedis(redisIP, redisPort))
		{
			jedis.auth(redisPassword);
			jedis.select(redisDb);
			JSONObject request = new JSONObject().put("oid", oid).put("code", code).put("time", time);
			System.out.println(request.toString());
			jedis.rpush("order_result_ok", request.toString());
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	public static void main(String[] args) throws Exception
	{
		String stringGoods = GoodApi.goodsList("1");
		JSONArray jsonGoods = new JSONObject(stringGoods).getJSONArray("goods");
		Good good1 = new Good(jsonGoods.getJSONObject(0));
		Good good2 = new Good(jsonGoods.getJSONObject(1));
		ArrayList<Item> itmList = new ArrayList<>();
		itmList.add(new Item(good1, 4, 1));
		itmList.add(new Item(good2, 4, 1));
		OrderApi.shopOrder(1, 889966, 11432923, BigInteger.valueOf(001), "毛001", 11432923, BigInteger.valueOf(001),
				"毛001", Instant.now().getEpochSecond(), itmList, "1.1.1.2");
		Thread.sleep(10000);
		String s = OrderApi.orderResult(889966);
		System.out.println(s);
		String oid = new JSONObject(s).getString("oid");
		OrderApi.orderResultOK(oid, 1, Instant.now().getEpochSecond());
		PurseApi.msQueueResult(889966);
	}

}
